package com.mdb.open.trade.resource.trade;

/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfInwardClaimService.java
* Author:   you.yichao
* Date:     2019-07-02
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-07-02          V1.00        代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.api.enums.OrderByEnum;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.bc.web.RepeatSubmitTypeEnum;
import com.mdb.bc.web.WebProperties;
import com.mdb.bc.web.annotation.DenyRepeatSubmit;
import com.mdb.bc.web.support.SuperResources;
import com.mdb.open.common.enums.trade.status.AttachFileStatusEnum;
import com.mdb.open.common.enums.trade.type.AttachFileTypeEnum;
import com.mdb.open.trade.constant.trade.Fields;
import com.mdb.open.trade.domain.bo.trade.ProcessInwardClaimBo;
import com.mdb.open.trade.domain.bo.trade.QueryAttachListByNoListBo;
import com.mdb.open.trade.domain.bo.trade.QueryInwardClaimPageListBo;
import com.mdb.open.trade.domain.dto.trade.ProcessInwardClaimDto;
import com.mdb.open.trade.domain.dto.trade.QueryInwardClaimPageListDto;
import com.mdb.open.trade.domain.ko.trade.InwardClaimPageListKo;
import com.mdb.open.trade.domain.po.trade.AttachFile;
import com.mdb.open.trade.domain.vo.trade.FileInfoVo;
import com.mdb.open.trade.domain.vo.trade.InwardClaimPageListVo;
import com.mdb.open.trade.facade.trade.InwardClaimFacade;
import com.mdb.open.trade.service.trade.TfAttachFileService;
import com.mdb.open.trade.service.trade.TfInwardClaimService;
import com.mdb.open.trade.service.trade.TfInwardRemittanceService;
import com.mdb.user.LoginUserInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 类描述：
 *  汇入认领表 前端控制器
 *
 * @author you.yichao
 * @date 2019-07-02
 */
@RestController
@RequestMapping("tf/inclaim")
@Api(tags = "贸易金融资金汇入认领")
public class InwardClaimResource extends SuperResources {

    @Autowired
    LoginUserInfoHelper loginUserInfoHelper;

    @Autowired
    TfInwardClaimService tfInwardClaimService;

    @Autowired
    TfAttachFileService tfAttachFileService;

    @Autowired
    TfInwardRemittanceService tfInwardRemittanceService;

    @Autowired
    InwardClaimFacade inwardClaimFacade;

    @Autowired
    WebProperties webProperties;


    @LogAnnotation
    @ApiOperation("汇入认领表列表 - 分页查询")
    @GetMapping("/page_list")
    public ListResultEx<InwardClaimPageListVo> findByCondition(@ModelAttribute QueryInwardClaimPageListDto dto) {


        QueryInwardClaimPageListBo bo = new QueryInwardClaimPageListBo();
        bo.setCustomerCode(loginUserInfoHelper.getCustomerCode(true));
        bo.setStartDate(dto.getStartDate());
        bo.setMerchantNo(dto.getMerchantNo());
        bo.setReceiveAcc(dto.getReceiveAcc());
        bo.setEndDate(dto.getEndDate());
        bo.setRemitAcc(dto.getRemitAcc());
        bo.setBusinessNo(dto.getBusinessNo());
        bo.setStatus(dto.getStatus());
        bo.setFileStatus(dto.getFileStatus());
        bo.setPageNo(dto.getPageNo());
        bo.setSort(dto.getSort());
        bo.setPageSize(dto.getPageSize());
        bo.setOrder(dto.getOrder());

        if (ObjectUtil.isEmpty(dto.getSort())) {
            bo.setSort(Fields.ID);
            bo.setOrder(OrderByEnum.DESC);
        }

        //分页查询
        IPage<InwardClaimPageListKo> page = tfInwardClaimService.pageQuery(bo);

        //查询附件信息
        List<String> bizOrderNoList = page.getRecords().stream().map(InwardClaimPageListKo::getClaimNo)
                .collect(Collectors.toList());

        QueryAttachListByNoListBo attachByTypeBo = new QueryAttachListByNoListBo();
        attachByTypeBo.setType(AttachFileTypeEnum.CLAIM_SUPPLEMENTARY_EXPLANATION.getValue());
        attachByTypeBo.setBizOrderNo(bizOrderNoList);
        attachByTypeBo.setStatus(AttachFileStatusEnum.NORMAL.getValue());
        List<AttachFile> fileList = tfAttachFileService.queryAttachByNoList(attachByTypeBo);


        List<InwardClaimPageListVo> resultList = Lists.newArrayList();
        for (InwardClaimPageListKo ko : page.getRecords()) {

            InwardClaimPageListVo vo = new InwardClaimPageListVo();
            vo.setRemitNo(ko.getRemitNo());
            vo.setReceiveAcc(ko.getReceiveAcc());
            vo.setBankInwardNo(ko.getBankInwardNo());
            vo.setRemitAcc(ko.getRemitAcc());
            vo.setRemitBank(ko.getRemitBank());
            vo.setRemitDate(ko.getRemitDate());
            vo.setRemitAmount(ko.getRemitAmount());
            vo.setClaimNo(ko.getClaimNo());
            vo.setRemark(ko.getRemark());
            vo.setDetails(ko.getDetails());
            vo.setChargeBearer(ko.getChargeBearer());
            vo.setCustomerCode(ko.getCustomerCode());
            vo.setCustomerName(ko.getCustomerName());
            vo.setMerchantName(ko.getMerchantName());
            vo.setMerchantNo(ko.getMerchantNo());
            vo.setBusinessNo(ko.getBusinessNo());
            vo.setClaimAmount(ko.getClaimAmount());
            vo.setCcy(ko.getCcy());
            vo.setPurposes(ko.getPurposes());
            vo.setClaimType(ko.getClaimType());
            vo.setBalance(ko.getBalance());
            vo.setClaimDate(ko.getClaimDate());
            vo.setStatus(ko.getStatus());
            vo.setFileStatus(ko.getFileStatus());
            vo.setCreateTime(ko.getCreateTime());
            vo.setUpdateTime(ko.getUpdateTime());

            Optional<AttachFile> matchingObject = fileList.stream()
                    .filter(p -> p.getBizOrderNo().equals(vo.getClaimNo()))
                    .filter(p -> p.getType().equals(AttachFileTypeEnum.CLAIM_SUPPLEMENTARY_EXPLANATION))
                    .findFirst();
            AttachFile file = matchingObject.orElseGet(AttachFile::new);
            vo.setAttachFile(new FileInfoVo(file.getAttachNo(), file.getFileUrl(), ko.getFileStatus()));


            resultList.add(vo);
        }
        return ListResultEx.ok(resultList, page.getTotal(), page.getCurrent(), page.getSize());

    }

    @LogAnnotation
    @ApiOperation("执行")
    @PostMapping("/claim_solve")
    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    public ResultEx claimSolve(@RequestBody ProcessInwardClaimDto dto) {

        ProcessInwardClaimBo bo = new ProcessInwardClaimBo();
        bo.setClaimItemDtoList(dto.getClaimItemDtoList());
        bo.setRemitNo(dto.getRemitNo());
        bo.setCustomerCode(loginUserInfoHelper.getCustomerCode(true));
        return inwardClaimFacade.claimSolve(bo);
    }

}
